﻿Imports Microsoft.Xna.Framework
Imports Runa.UI.Input
Imports Microsoft.Xna.Framework.Graphics
Imports Runa.UI.Styles

Namespace Global.Runa.UI.Controls

    Public Class Button
        Inherits ContentControl

        Public Property ButtonTintColor() As Color

        Private _Style As ButtonStyle

        Public Shadows Property Style() As ButtonStyle
            Get
                Return Me._Style
            End Get
            Set(ByVal value As ButtonStyle)
                If (Not Me._Style Is value) Then
                    Me._Style = value
                    Me.ButtonTintColor = Me.Style.ButtonTintColors(0)
                End If
            End Set
        End Property

        Public Sub New(ByVal Parent As UIElementBase)
            MyBase.New("", Parent)
            Me.Name = Me.GetType().Name & "_" & BytesToString(Guid.NewGuid().ToByteArray())
            Me.Style = New ButtonStyle("", Me.Game)
            Me.Style.Skin = Me.ResourceContentManager.LoadTexture2D("skin.xnb")
        End Sub

        Public Sub New(ByVal Name As String, ByVal Parent As UIElementBase, Optional ByVal StyleName As String = "Default")
            MyBase.New(Name, Parent)
            AddHandler MyBase.MouseLeftButtonDown, New MouseEventHandler(AddressOf Me.Button_MouseLeftButtonDown)
            AddHandler MyBase.MouseLeftButtonUp, New MouseEventHandler(AddressOf Me.Button_MouseLeftButtonUp)
            AddHandler MyBase.MouseEnter, New MouseEventHandler(AddressOf Me.Button_MouseEnter)
            AddHandler MyBase.MouseLeave, New MouseEventHandler(AddressOf Me.Button_MouseLeave)
            Me.LoadStyle(StyleName)
        End Sub

        Private Sub Button_MouseEnter(ByVal sender As Object, ByVal e As MouseInputEventArgs)
            Me.ButtonTintColor = Me.Style.ButtonTintColors(1)
        End Sub

        Private Sub Button_MouseLeave(ByVal sender As Object, ByVal e As MouseInputEventArgs)
            Me.ButtonTintColor = Me.Style.ButtonTintColors(0)
        End Sub

        Private Sub Button_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseInputEventArgs)
            Me.ButtonTintColor = Me.Style.ButtonTintColors(3)
        End Sub

        Private Sub Button_MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseInputEventArgs)
            Me.ButtonTintColor = Me.Style.ButtonTintColors(2)
        End Sub

        Public Overrides Sub Draw(ByVal SpriteBatch As SpriteBatch)
            Dim bounds As Rectangle = Me.AbsoluteBounds
            Dim tint As Color = Me.ButtonTintColor * MyBase.Opacity
            SpriteBatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend)
            SpriteBatch.Draw(Me.Style.Skin.Texture, New Rectangle(bounds.X, bounds.Y, Me.Style.SkinButtonLeft.Width, bounds.Height), New Nullable(Of Rectangle)(Me.Style.SkinButtonLeft), tint, Me.AbsoluteRotation, MyBase.Orgin, MyBase.SpriteEffect, 0.5!)
            SpriteBatch.Draw(Me.Style.Skin.Texture, New Rectangle(((bounds.X + bounds.Width) - Me.Style.SkinButtonRight.Width), bounds.Y, Me.Style.SkinButtonRight.Width, bounds.Height), New Nullable(Of Rectangle)(Me.Style.SkinButtonRight), tint, Me.AbsoluteRotation, MyBase.Orgin, MyBase.SpriteEffect, 0.5!)
            SpriteBatch.Draw(Me.Style.Skin.Texture, New Rectangle((bounds.X + Me.Style.SkinButtonLeft.Width), bounds.Y, ((bounds.Width - Me.Style.SkinButtonLeft.Width) - Me.Style.SkinButtonRight.Width), bounds.Height), New Nullable(Of Rectangle)(Me.Style.SkinButtonMiddle), tint, Me.AbsoluteRotation, MyBase.Orgin, MyBase.SpriteEffect, 0.5!)
            SpriteBatch.End()
            MyBase.Draw(SpriteBatch)
        End Sub

        Private Sub LoadStyle(ByVal StyleName As String)
        End Sub

    End Class

End Namespace